﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Query
{
    /// <summary>
    /// Listview 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        protected string keyString;
        protected int Range;
        protected int SearchType;
        protected string UserName;

        protected override void OnLoad(EventArgs e)
        {
            keyString = (Request.QueryString["key"] != null) ? Request.QueryString["key"] : "";
            UserName = SiteContext.Current.UserName;
            Range = Int32.Parse((Request.QueryString["Range"] != null) ? Request.QueryString["Range"] : "0");
            SearchType = Int32.Parse((Request.QueryString["SearchType"] != null) ? Request.QueryString["SearchType"] : "0");
            if (!IsPostBack)
            {
                BindGrid();
            }

            base.OnLoad(e);
        }

        private void FileSearch(string catalog)
        {
            var mySQL = new Database();
            var ds = new DataSet();
            String myConnStr = "Provider=MSIDXS; Data Source=" + catalog + ";";
            String mySql = "SELECT FileName, Path, Characterization, Rank, Create, Size FROM SCOPE() WHERE CONTAINS ('" + keyString + "')> 0";
            var myConn = new OleDbConnection(myConnStr);

            var myAdapter = new OleDbDataAdapter(mySql, myConn);
            try
            {
                myAdapter.Fill(ds, "Search");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
            finally
            {
                myConn.Close();
            }

            for (int i = 0; i < ds.Tables["Search"].Rows.Count; i++)
            {
                DataRow myDr = ds.Tables["Search"].Rows[i];

                SqlParameter[] pparameters = {
                    Database.MakeInParam("@FileName", SqlDbType.NVarChar, 300, myDr["FileName"]),
                    Database.MakeInParam("@Path", SqlDbType.NVarChar, 300, myDr["Path"].ToString().Replace(Server.MapPath("..\\UnitiveDocument\\Document").Replace("/", "\\").ToLower(), "")),
                    Database.MakeInParam("@Characterization", SqlDbType.NText, 10000, myDr["Characterization"]),
                    Database.MakeInParam("@Rank", SqlDbType.NVarChar, 300, myDr["Rank"]),
                    Database.MakeInParam("@Create", SqlDbType.DateTime, 10, myDr["Create"]),
                    Database.MakeInParam("@Size", SqlDbType.Int, 4, myDr["Size"])
                };
                mySQL.RunProc("dbo.sproc_AddScope", pparameters);
            }
        }

        public string GetUserRealName(string UserStr)
        {
            return Components.Staff.GetRealNameForStaffList(UserStr, 1);
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgDocList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion

        #region 数据绑定至DataGrid

        /// <summary>
        /// 将某用户的邮件取出绑定至DataGrid
        /// </summary>
        protected void BindGrid()
        {
            var mySQL = new Database();
            /*
			 *=========================================			  
			 *				处理文档			 
			 *========================================= 			 
			 */

            if ((SearchType & 0x01) == 0x01)
            {
                dgDocList.Visible = true;
                if ((Range & 0x08) == 0x08)
                {
                    FileSearch("UDS_Doc");
                }


                SqlParameter[] parameters = {
                    Database.MakeInParam("@UserName", SqlDbType.NVarChar, 300, UserName),
                    Database.MakeInParam("@KeyString", SqlDbType.NVarChar, 300, keyString),
                    Database.MakeInParam("@SearchRange", SqlDbType.Int, 4, Range),
                    Database.MakeInParam("@ViewDocRightCode", SqlDbType.Int, 4, 10),
                    Database.MakeInParam("@Inherit", SqlDbType.Bit, 1, 0)
                };
                DataTable dt;
                using (SqlDataReader dr = mySQL.RunDataReader("dbo.sproc_FindDoc", parameters))
                {
                    dt = Tools.ConvertDataReaderToDataTable(dr);
                    dr.Close();
                }


                dgDocList.DataSource = dt.DefaultView;
                dgDocList.DataBind();
            }
            else
            {
                dgDocList.Visible = false;
                tabDoc.Visible = false;
            }

            /*
			 *=========================================			  
			 *				处理邮件			 
			 *========================================= 
			 */
            if ((SearchType & 0x02) == 0x02)
            {
                dgMailList.Visible = true;
                if ((Range & 0x08) == 0x08)
                {
                    FileSearch("UDS_Mail");
                }

                SqlParameter[] parameters = {
                    Database.MakeInParam("@UserName", SqlDbType.NVarChar, 300, UserName),
                    Database.MakeInParam("@KeyString", SqlDbType.NVarChar, 300, keyString),
                    Database.MakeInParam("@SearchRange", SqlDbType.Int, 4, Range)
                };
                DataTable dt;
                using (SqlDataReader dr = mySQL.RunDataReader("dbo.sproc_FindMail", parameters))
                {
                    dt = Tools.ConvertDataReaderToDataTable(dr);
                    dr.Close();
                }


                dgMailList.DataSource = dt.DefaultView;
                dgMailList.DataBind();
            }
            else
            {
                tabMail.Visible = false;
                dgMailList.Visible = false;
            }
        }

        #endregion
    }
}